﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MSNPSharp;
using CGLib;

namespace TienLen
{
    public partial class MSNConvo : UserControl
    {
        private Contact remoteContact = null;

        public MSNConvo(Contact contact, string txtSend)
        {
            InitializeComponent();
            btnExit.Image = Image.FromFile(Application.StartupPath + @"\img\other\kickb-normal.png");

            this.remoteContact = contact;

            #region MSN Events

            remoteContact.StatusChanged += new EventHandler<StatusChangedEventArgs>(contact_StatusChanged);
            remoteContact.PersonalMessageChanged += new EventHandler<EventArgs>(contact_PMNameChanged);
            remoteContact.ScreenNameChanged += new EventHandler<EventArgs>(contact_PMNameChanged);

            SvrFunc.MSN.MessageManager.TextMessageReceived += new EventHandler<TextMessageArrivedEventArgs>(MessageManager_TextMessageReceived);

            #endregion

            #region MSN User Info

            picOwner.Image = SvrFunc.MSN.Owner.DisplayImage.Image;

            if (TienLen.Properties.Settings.Default.opt_msndp)
            {
                contact.DisplayImageContextChanged += new EventHandler<DisplayImageChangedEventArgs>(MSNConvo_DisplayImageContextChanged);
                contact.DisplayImageChanged += new EventHandler<DisplayImageChangedEventArgs>(MSNConvo_DisplayImageChanged);

                if (contact.DisplayImage != contact.UserTileLocation)
                    SvrFunc.MSN.RequestMsnObject(contact, contact.DisplayImage);
                else
                    picUsr.Image = contact.DisplayImage.Image;
            }

            txtName.Text = contact.Name;
            txtPM.Text = contact.PersonalMessage.Message;
            SetStatus();

            #endregion

            // If the conversation was started because of the remote user, add the message the user has sent.
            if (txtSend != string.Empty) AppendText(contact.Name, txtSend);
        }

        private void MessageManager_TextMessageReceived(object sender, TextMessageArrivedEventArgs e)
        {
            if (e.OriginalSender == remoteContact)
            {
                bool success;
                MSNPacket packet = new MSNPacket((e as TextMessageArrivedEventArgs).TextMessage.Text, out success);

                if (success) return;

                cbox.BeginInvoke(new MethodInvoker(delegate() 
                    { AppendText(e.Sender.Name, (e as TextMessageArrivedEventArgs).TextMessage.Text); }));
            }
        }

        private void contact_StatusChanged(object sender, StatusChangedEventArgs e)
        {
            Invoke(new MethodInvoker(SetStatus));
        }

        private void contact_PMNameChanged(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new EventHandler<EventArgs>(contact_PMNameChanged), sender, e);
                return;
            }

            txtPM.Text = remoteContact.PersonalMessage.Message;
            txtName.Text = remoteContact.Name;

            if (Parent != null) Parent.Text = remoteContact.Name;
        }

        /// <summary>
        /// Set the status information to picStatus.
        /// </summary>
        private void SetStatus()
        {
            switch (remoteContact.Status)
            {
                case PresenceStatus.Online :
                    picStatus.Image = Image.FromFile(Application.StartupPath + @"\img\other\wlm-online.png");
                    break;
                case PresenceStatus.Busy :
                    picStatus.Image = Image.FromFile(Application.StartupPath + @"\img\other\wlm-busy.png");
                    break;
                case PresenceStatus.Away :
                    picStatus.Image = Image.FromFile(Application.StartupPath + @"\img\other\wlm-away.png");
                    break;
                case PresenceStatus.Offline :
                    picStatus.Image = Image.FromFile(Application.StartupPath + @"\img\other\wlm-offline.png");
                    break;
            }

            tpInfo.SetToolTip(picStatus, remoteContact.Name + " is currently " + remoteContact.Status.ToString());
        }

        private void MSNConvo_DisplayImageContextChanged(object sender, DisplayImageChangedEventArgs e)
        {
            SvrFunc.MSN.RequestMsnObject(sender as Contact, new DisplayImage());
        }

        private void MSNConvo_DisplayImageChanged(object sender, DisplayImageChangedEventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new EventHandler<DisplayImageChangedEventArgs>(MSNConvo_DisplayImageChanged), sender, e);
                return;
            }

            picUsr.Image = e.NewDisplayImage.Image;
        }

        private void AppendText(string name, string text)
        {
            if (SvrFunc.MSN.Owner.Name == name)
                cbox.SelectionColor = Color.Red;
            else cbox.SelectionColor = Color.Blue;

            cbox.SelectionFont = new Font(cbox.Font, FontStyle.Bold);
            cbox.AppendText(name + ": ");


            cbox.SelectionColor = Color.Black;
            cbox.SelectionFont = new Font(cbox.Font, FontStyle.Regular);
            cbox.AppendText(text + "\r\n");
        }

        private void cwrite_TextChanged(object sender, EventArgs e)
        {
            csend.Enabled = !string.IsNullOrEmpty(cwrite.Text);
        }

        private void csend_Click(object sender, EventArgs e)
        {
            if (remoteContact.Online)
                remoteContact.SendMessage(new TextMessage(cwrite.Text));
            else
                SvrFunc.MSN.OIMService.SendOIMMessage(remoteContact, new TextMessage(cwrite.Text));

            AppendText(SvrFunc.MSN.Owner.Name, cwrite.Text);

            cwrite.Multiline = false;
            cwrite.Text = string.Empty;
            cwrite.Multiline = true;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            if (TienLen.Properties.Settings.Default.opt_msndp)
            {
                remoteContact.DisplayImageContextChanged -= MSNConvo_DisplayImageContextChanged;
                remoteContact.DisplayImageChanged -= MSNConvo_DisplayImageChanged;
            }

            remoteContact.PersonalMessageChanged -= contact_PMNameChanged;
            remoteContact.StatusChanged -= contact_StatusChanged;
            remoteContact.ScreenNameChanged -= contact_PMNameChanged;

            SvrFunc.MSN.MessageManager.TextMessageReceived -= MessageManager_TextMessageReceived;

            (this.Parent.Parent as TabControl).TabPages.Remove(Parent as TabPage);
        }

        private void cwrite_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r' && ModifierKeys == Keys.None) csend_Click(null, EventArgs.Empty);
        }
    }
}
